from bs4 import BeautifulSoup
from SDset import SymbolicDisjointSet
from math import sqrt
import json

def getID_without_time(filename):
    infile = open("sourcedata/" + filename, 'r')
    txt = infile.read()
    source_dict = json.loads(txt.decode("utf8", 'replace'))
    nodes = source_dict.get('node')
    edges = source_dict.get('edge')
    id2matrixId = dict()
    id2year = dict()
    matrixId_count = 1
    node_keys = list(nodes)
    for node_key in node_keys:
        node = nodes[node_key]
        if id2matrixId.get(node_key) == None:
            id2matrixId[node_key] = matrixId_count
            matrixId_count += 1
        id2year[node_key] = node['year']
    outfile = open("sourcedata/matrixID.json", "w")
    outstring = json.dumps(id2matrixId)
    outfile.write(outstring)
    outfile.close()
    outfile = open("sourcedata/vector.txt", "w")
    for i in range(1, matrixId_count):
        outfile.write("1\n")
    outfile.close()
    outfile = open("sourcedata/sparse.txt", "w")
    for edge in edges:
        tmpstring = str(id2matrixId[edge['source']]) + "," + str(id2matrixId[edge['target']]) + ",1.0\n"
        outfile.write(tmpstring)
    outfile.close()

def getID(filename):
    infile = open("sourcedata/" + filename, 'r')
    txt = infile.read()
    source_dict = json.loads(txt.decode("utf8", 'replace'))
    nodes = source_dict.get('node')
    edges = source_dict.get('edge')
    id2matrixId = dict()
    id2year = dict()
    matrixId_count = 1
    node_keys = list(nodes)
    for node_key in node_keys:
        node = nodes[node_key]
        if id2matrixId.get(node_key) == None:
            id2matrixId[node_key] = matrixId_count
            matrixId_count += 1
        id2year[node_key] = node['year']
    outfile = open("sourcedata/matrixID.json", "w")
    outstring = json.dumps(id2matrixId)
    outfile.write(outstring)
    outfile.close()
    outfile = open("sourcedata/vector.txt", "w")
    for i in range(1, matrixId_count):
        outfile.write("1\n")
    outfile.close()
    outfile = open("sourcedata/sparse.txt", "w")
    for edge in edges:
        tmpstring = str(id2matrixId[edge['source']]) + "," + str(id2matrixId[edge['target']]) + "," + str(2016 - id2year[edge['target']]) + ".0\n"
        outfile.write(tmpstring)
    outfile.close()
    # done
